<!DOCTYPE html>
<html>
<head>
  <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
  <title>The source code</title>
  <link href="../resources/prettify/prettify.css" type="text/css" rel="stylesheet" />
  <script type="text/javascript" src="../resources/prettify/prettify.js"></script>
  <style type="text/css">
    .highlight { display: block; background-color: #ddd; }
  </style>
  <script type="text/javascript">
    function highlight() {
      document.getElementById(location.hash.replace(/#/, "")).className = "highlight";
    }
  </script>
</head>
<body onload="prettyPrint(); highlight();">
  <pre class="prettyprint lang-js"><span id='global-property-'>/**
</span> * focus management
 * @ignore
 * @author yiminghe@gmail.com
 */
KISSY.add(&quot;editor/focusManager&quot;, function (S, Editor) {

    var INSTANCES = {},
        timer,
        currentInstance,
<span id='KISSY-Editor-focusManager'>        /**
</span>         * focus management for all editor instances.
         * @class KISSY.Editor.focusManager
         * @singleton
         * @private
         */
            focusManager = {
<span id='KISSY-Editor-focusManager-method-currentInstance'>            /**
</span>             * get current focused editor instance
             */
            currentInstance: function () {
                return currentInstance;
            },
<span id='KISSY-Editor-focusManager-method-getInstance'>            /**
</span>             * get editor instance by editor id
             * @param id {string}
             */
            getInstance: function (id) {
                return INSTANCES[id];
            },
<span id='KISSY-Editor-focusManager-method-register'>            /**
</span>             * register editor within focus manager
             * @param editor
             */
            register: function (editor) {
                INSTANCES[editor.get('id')] = editor;
            },
<span id='KISSY-Editor-focusManager-method-add'>            /**
</span>             * monitor editor focus and register editor
             * @param editor
             */
            add: function (editor) {
                this.register(editor);
                editor.get(&quot;window&quot;).on(&quot;focus&quot;, focus, editor)
                    .on(&quot;blur&quot;, blur, editor);
            },
<span id='KISSY-Editor-focusManager-method-remove'>            /**
</span>             * remove editor from focus manager
             * @param editor
             */
            remove: function (editor) {
                delete INSTANCES[editor.get('id')];
                editor.get(&quot;window&quot;).detach(&quot;focus&quot;, focus, editor)
                    .detach(&quot;blur&quot;, blur, editor);
            }
        },
        TRUE = true,
        FALSE = false,
        NULL = null;

    function focus() {
        var editor = this;
        editor.__iframeFocus = TRUE;
        currentInstance = editor;
        if (timer) {
            clearTimeout(timer);
        }
        timer = setTimeout(function () {
            editor.fire(&quot;focus&quot;);
        }, 30);
    }

    function blur() {
        var editor = this;
        editor.__iframeFocus = FALSE;
        currentInstance = NULL;
        if (timer) {
            clearTimeout(timer);
        }
        /*
         Note that this functions acts asynchronously with a delay of 30ms to
         avoid subsequent blur/focus effects.
         */
        timer = setTimeout(function () {
            editor.fire(&quot;blur&quot;);
        }, 30);
    }

    Editor.focusManager = focusManager;
    Editor.getInstances = function () {
        return INSTANCES;
    };

    return focusManager;
}, {
    requires: ['./base', './dom']
});
</pre>
</body>
</html>
